var num1 = 1; //到第几个td
var timer = null;//定时器
//var num2 = 2;//转几圈
var num2 = randomNum(2, 5);//转几圈随机数，最大5圈最小2圈

//var zidinyi = 6;//到哪一格停下来
var arr = [1, 3, 5, 7];
var zidinyi = arr[randomNum(0, 3)];//到哪一格停下来，数组中的数

var num3 = 3;//总共有几次机会


var jinzhi = true;//禁止函数运行时再次点击开始

$("#start").text("还剩(" + num3 + "次)");


//生成从minNum到maxNum的随机数
function randomNum(minNum, maxNum) {
    switch (arguments.length) {
        case 1:
            return parseInt(Math.random() * minNum + 1, 10);
            break;
        case 2:
            return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
            break;
        default:
            return 0;
            break;
    }
}

$("#btn").click(function () {
    if (!jinzhi) {
        return;
    }
    jinzhi = false;

    num3--;
    if (num3 < 0) {
        alert("你抽奖次数已用完！");
        jinzhi = true;
        return;//结束函数运行
    } else {
        $("#start").text("还剩(" + num3 + "次)");
    }

    timer = setInterval(function () {

        if (num2 <= 0 && zidinyi + 1 === num1) {
            clearInterval(timer);//整个函数完成结束
            num1 = 1;//复位到第一个
            num2 = randomNum(2, 5);//转几圈，重新给个随机数
            //alert("恭喜你抽到" + zidinyi + "个");//返回抽奖结果

                if (zidinyi === 1) {
                    setTimeout(function () {
                        $("#qizhe").show();
                    }, 500);
                } else if (zidinyi === 3) {
                    setTimeout(function () {
                        $("#wuzhe").show();
                    }, 500);
                } else if (zidinyi === 5) {
                    setTimeout(function () {
                        $("#jiuzhe").show();
                    }, 500);
                } else if (zidinyi === 7) {
                    setTimeout(function () {
                        $("#erzhe").show();
                    }, 500);
                }



            zidinyi = arr[randomNum(0, 3)];//重新获取到哪一格停下来，数组中的数
            jinzhi = true;
        } else {
            if (num1 === 9) {
                $(".wu>img").attr("src", "../image/guang1.png");
            } else if (num1 === 2) {
                $(".wu>img").attr("src", "../image/guang2.png");
            } else if (num1 === 3) {
                $(".wu>img").attr("src", "../image/guang3.png");
            } else if (num1 === 4) {
                $(".wu>img").attr("src", "../image/guang4.png");
            } else if (num1 === 5) {
                $(".wu>img").attr("src", "../image/guang5.png");
            } else if (num1 === 6) {
                $(".wu>img").attr("src", "../image/guang6.png");
            } else if (num1 === 7) {
                $(".wu>img").attr("src", "../image/guang7.png");
            } else if (num1 === 8) {
                $(".wu>img").attr("src", "../image/guang8.png");
            }
            if (num1 === 9) { //到第九个的时候，立即复原到第一个
                //clearInterval(timer);
                num1 = 1;
                num2--;
            }
            /*else {*/  //去掉eles 不管if（num1 === 9 ）是不是true都会执行，解决卡顿效果
            $(".td").removeClass("on");
            $(".td" + num1).addClass("on");
            //$(".td"+num1).css("background","url(zjk.png) no-repeat");
            num1++;
            /*}*/
        }
    }, 100);
});


$(".cha").click(function () {
    $(".yc").hide();
});

$(".bottn").click(function(){
    alert("恭喜你领取成功！！")
});

$(".guize").click(function(){
    $("#guize").show();
});






